*****************************************************************************************
*REPLICATION FILE FOR ALL RESULTS IN PAPER - REMEMBER TO CHANGE ALL PATHS FOR FILE TO RUN
*****************************************************************************************
*Figure 1 was created using Stata's Graph Editor

clear all
set mem 700m
set more off
est clear


use replication_data, clear
*label new instrument (30 percent deviation from rainfall)
label var perc_rainfall30 "rainfall \% deviation, 30\% cutoff"


*Generate oblast fixed effects
cap encode oblast, gen (oblast_num)
rename oblast_num _oblast_num
cap tabulate oblast, gen (oblast_d)
global oblastdum  oblast_*



*Generate region fixed effects
cap encode region , gen (region_num)
rename region_num _region_num
cap tabulate region , gen (region_d)
global regiondum  region_*


***LISTS OF CONTROLS: GEOGRAPHY, ECONOMY, CIVIL SOCIETY, POLITICS, MAYOR

local geography distance_spcity elevation naklon_relef longitude latitude ndist1km region_d1 region_d2 region_d3 region_d4 region_d5 region_d6 
local geography2 distance_spcity elevation naklon_relef longitude latitude ndist1km
local economy log_munic_inc_cap unemp03 netprivincome
local civil_society circan_cap  univ_share urban_p turnout
local local_gov mayor_trcoal councilmaj_trcoal mayor_council_trcoal
local local_gov_dps mayor_dps	 councilmaj_dps	   mayor_council_dps
local local_gov_all mayor_trcoal councilmaj_trcoal mayor_council_trcoal mayor_dps	 councilmaj_dps	   mayor_council_dps
local mayor margin_03 mayor_pol_pre_2003 


				local instrument perc_rainfall30
				local label : var label `instrument'

***********************************
*MERGE WITH TOPUP 2003, POPULATION
merge 1:1 nsicode using "C:\NikolovE\Dropbox\BG Corruption _MILENA\_REPLICATION_30perc_cutoff\topup2003.dta"
drop _merge

*generate ln topup per capita (no need to add one as it is always higher than 0)
gen log_topup2003_cap = ln(topup2003/population)
label var log_topup2003_cap "Log budget topup per capita, 2003"

gen topup2003_cap = topup2003/population
label var topup2003_cap "Budget topup per capita, 2003"
				

gen audited = 0 
replace audited = 1 if spnar!=.


******BASELINE TABLE********
*rsquared reported only for ols

				eststo ols_1: reg spnar  log_funds_total_cap `geography' `economy' `civil_society'  				, robust
				estadd scalar rsq 		e(r2)				 : ols_1

				eststo ols_2: reg spnar  log_funds_total_cap `geography' `economy' `civil_society'  `local_gov'		, robust
				estadd scalar rsq 		e(r2)				 : ols_2
				
				eststo iv_1: ivreg29  spnar `geography' `economy' `civil_society'  			 (log_funds_total_cap =  `instrument' ) , robust savefirst savefprefix (first) first
				gen sample_iv1 = e(sample)
				estadd scalar wid_stat 		e(widstat)				 : iv_1
				est restore firstlog_funds_total_cap
				scalar fs_b = _b[`instrument']
				scalar fs_se = _se[`instrument']
				estadd scalar fs_b=fs_b								 : iv_1
				estadd local fs_se = "("+string(fs_se,"%7.3f")+")"   : iv_1
				

				eststo iv_2: ivreg29  spnar `geography' `economy' `civil_society' `local_gov' (log_funds_total_cap =  `instrument' ) , robust savefirst savefprefix (first) first	
				gen sample_iv2 = e(sample)
				estadd scalar wid_stat 		e(widstat)				: iv_2
			    est restore firstlog_funds_total_cap
				scalar fs_b = _b[`instrument']
				scalar fs_se = _se[`instrument']
				estadd scalar fs_b=fs_b 							: iv_2
				estadd local fs_se = "("+string(fs_se,"%7.3f")+")"  : iv_2
				

				ivpoisson gmm spnar `geography' `economy' `civil_society' `local_gov' `mayor' (log_funds_total_cap =  `instrument') , twostep 
				eststo iv_5: margins, dydx(log_funds_total_cap elevation `economy' `civil_society' `local_gov' )  post force
			   
		
esttab 	using table1.tex, label replace se star(* 0.1 ** 0.05 *** 0.01) booktabs  nonotes ///		
		alignment(D{.}{.}{-1})   ///
		substitute("}{l}{\footnotesize" "}{p{\linewidth}}{\footnotesize" "\begin{table}[htbp]\centering" "" "\end{table}" "" "fs_b" "rainfall \% deviation, 10\% cutoff") ///
		mgroups("OLS" "IV" "IV-Poisson", pattern(1 0 1 0 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
		indicate( "Geography = distance_spcity elevation naklon_relef longitude latitude ndist1km" "Economic conditions = `economy'" "Civil society = `civil_society'" "Composition of local gov. = `local_gov'" , labels("\checkmark"  "")) ///
		drop(_cons region* o.*) ///
		order() ///
		stats(wid_stat fs_b fs_se rsq N, labels("F-stat." "`label'" " " "R-sq" "Number of observations")) ///
		
est clear
	
	drop sample_iv*
		
	
******FIRST STAGE RESULTS********

				eststo iv_1: reg  log_funds_total_cap `instrument' `geography' `economy' `civil_society' 					if sample_iv1==1 ,robust  					 
				estadd scalar rsq 		e(r2)				 : iv_1
				eststo iv_2: reg  log_funds_total_cap `instrument' `geography' `economy' `civil_society' `local_gov' 		if sample_iv2==1 ,robust 		 
				estadd scalar rsq 		e(r2)				 : iv_2
				eststo iv_3: reg  log_funds_total_cap `instrument' `geography' `economy' `civil_society' `local_gov_dps' 	if sample_iv2==1 ,robust 		 
				estadd scalar rsq 		e(r2)				 : iv_3
				
esttab 	using table2.tex, label replace se star(* 0.1 ** 0.05 *** 0.01) booktabs  nonotes ///		
		alignment(D{.}{.}{-1})   ///
		substitute("}{l}{\footnotesize" "}{p{\linewidth}}{\footnotesize" "\begin{table}[htbp]\centering" "" "\end{table}" "") ///
		indicate( "Region dummies =  region_* ", labels("\checkmark"  "")) ///
		drop(_cons o.*) ///
		order(`instrument') ///
		stats(rsq N, labels( "R-sq" "Number of observations")) ///
		
est clear	



avplot `instrument',  ytitle ("log funds per capita") xtitle ("rainfall % deviation, 30% cutoff") mlabel(municipality_nsiname)


graph save Graph "C:\NikolovE\Dropbox\BG Corruption _MILENA\_REPLICATION_30perc_cutoff\avplot_firststage.gph", replace


*******RE-ELECTION****************


******FIRST STEP: GENERATED FITTED VALUES FOR CORRUPTION*****

			ivreg2  spnar `geography' `economy' `civil_society' `local_gov'  		 (log_funds_total_cap =  `instrument'   ) , robust
			_predict spnar_fitted if spnar!=. , xb
			label var spnar_fitted "predicted BNAA"


*****SECOND STEP: REELECTION REGRESSIONS
		
				probit candidate_in2007  spnar_fitted turnout_07percent	`mayor' `geography' `economy' `civil_society' 			 , robust
		eststo: margins,        dydx(  	 spnar_fitted turnout_07percent	`mayor' 			`economy' `civil_society' 			  elevation) post		
				
				probit candidate_in2007  spnar_fitted turnout_07percent	`mayor' `geography' `economy' `civil_society' `local_gov' , robust
		eststo: margins,        dydx(  	 spnar_fitted turnout_07percent	`mayor' 			`economy' `civil_society' `local_gov' elevation) post		
				
				
				probit mayor_reelected   spnar_fitted  turnout_07percent	`mayor' `geography' `economy' `civil_society' 			 , robust
		eststo: margins,        dydx(  	 spnar_fitted  turnout_07percent	`mayor' 			`economy' `civil_society' 			  elevation) post		
				
				probit mayor_reelected   spnar_fitted  turnout_07percent	`mayor' `geography' `economy' `civil_society' `local_gov' , robust
		eststo: margins,        dydx(  	 spnar_fitted  turnout_07percent	`mayor' 			`economy' `civil_society' `local_gov' elevation) post		
		
		
		
esttab 	using table3.tex,r2 label replace se star(* 0.1 ** 0.05 *** 0.01) booktabs  nonotes ///		
		alignment(D{.}{.}{-1})   ///
		substitute("}{l}{\footnotesize" "}{p{\linewidth}}{\footnotesize" "\begin{table}[htbp]\centering" "" "\end{table}" ""  "&\multicolumn{1}{c}{} &\multicolumn{1}{c}{} &\multicolumn{1}{c}{} &\multicolumn{1}{c}{}"   "&\multicolumn{1}{c}{candidate in 2007}&\multicolumn{1}{c}{candidate in 2007}&\multicolumn{1}{c}{reelected in 2007}&\multicolumn{1}{c}{reelected in 2007}" ) ///
		indicate( "Geography = elevation" "Economic conditions = `economy'" "Civil society = `civil_society'" "Composition of local gov. = `local_gov'" , labels("\checkmark"  "")) ///		
		drop() ///
		order(spnar_fitted ) ///
		stats( N, labels( "Number of observations")) ///

est clear
		

****** CLOSE ELECTIONS, MARGIN 03 LESS THAN 10%************
preserve
		keep if margin_03<=0.1
		
				
		
				probit candidate_in2007  spnar_fitted turnout_07percent	`mayor' `geography' `economy' `civil_society' 			 , robust
		eststo: margins,        dydx(  	 spnar_fitted turnout_07percent	`mayor' 			`economy' `civil_society' 			  elevation) post		
				
				probit candidate_in2007  spnar_fitted turnout_07percent	`mayor' `geography' `economy' `civil_society' `local_gov' , robust
		eststo: margins,        dydx(  	 spnar_fitted turnout_07percent	`mayor' 			`economy' `civil_society' `local_gov' elevation) post		
				
				
				probit mayor_reelected   spnar_fitted  turnout_07percent	`mayor' `geography' `economy' `civil_society' 			 , robust
		eststo: margins,        dydx(  	 spnar_fitted  turnout_07percent	`mayor' 			`economy' `civil_society' 			  elevation) post		
				
				probit mayor_reelected   spnar_fitted  turnout_07percent	`mayor' `geography' `economy' `civil_society' `local_gov' , robust
		eststo: margins,        dydx(  	 spnar_fitted  turnout_07percent	`mayor' 			`economy' `civil_society' `local_gov' elevation) post		
		
		
			
		
esttab 	using table4.tex,r2 label replace se star(* 0.1 ** 0.05 *** 0.01) booktabs  nonotes ///		
		alignment(D{.}{.}{-1})   ///
		substitute("}{l}{\footnotesize" "}{p{\linewidth}}{\footnotesize" "\begin{table}[htbp]\centering" "" "\end{table}" ""  "&\multicolumn{1}{c}{} &\multicolumn{1}{c}{} &\multicolumn{1}{c}{} &\multicolumn{1}{c}{}"   "&\multicolumn{1}{c}{candidate in 2007}&\multicolumn{1}{c}{candidate in 2007}&\multicolumn{1}{c}{reelected in 2007}&\multicolumn{1}{c}{reelected in 2007}" ) ///
		indicate( "Geography = elevation" "Economic conditions = `economy'" "Civil society = `civil_society'" "Composition of local gov. = `local_gov'" , labels("\checkmark"  "")) ///		
		drop() ///
		order(spnar_fitted ) ///
		stats( N, labels( "Number of observations")) ///

est clear		
					
		

